package com.nari.ysphd.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.jeremyfeinstein.utils.AppConstants;
import com.jeremyfeinstein.utils.ToastUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2018/3/8.
 */

public class APPUtils {

    //判断该机器是否为模拟器
    public static boolean isEmulator2(Context mContext) {
        String url = "tel:" + "12345678910";
        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        intent.setAction(Intent.ACTION_DIAL);
        // 是否可以处理跳转到拨号的 Intent
        boolean canResolveIntent = intent.resolveActivity(mContext.getPackageManager()) != null;

        return Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.toLowerCase().contains("vbox")
                || Build.FINGERPRINT.toLowerCase().contains("test-keys")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.SERIAL.equalsIgnoreCase("unknown")
                || Build.SERIAL.equalsIgnoreCase("android")
//                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk".equals(Build.PRODUCT)
                || getNetworkopera((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE))
                || !canResolveIntent;
    }

    private static boolean getNetworkopera(TelephonyManager telephonymanger) {
        String networkOperatorName = telephonymanger.getNetworkOperatorName();
        return ("android").equalsIgnoreCase(networkOperatorName.toLowerCase());
    }

    //隐藏软键盘
    public static void hidekeyboard(Activity activity, EditText et2) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        boolean isOpen = imm.isActive();//isOpen若返回true，则表示输入法打开
        if (isOpen) {
            imm.showSoftInput(et2, InputMethodManager.SHOW_FORCED);
            imm.hideSoftInputFromWindow(et2.getWindowToken(), 0); //强制隐藏键盘
        }
    }

    public static boolean isRootAvailable() {
        for (String pathDir : System.getenv(AppConstants.FILE_PATH).split(":")) {
            if (new File(pathDir, AppConstants.SU).exists()) {
                return true;
            }
        }
        return false;
    }

    public static boolean isRootGiven() {
        if (isRootAvailable()) {
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(new String[]{"su", "-c", "id"});
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String output = in.readLine();
                if (output != null && output.toLowerCase().contains("uid=0"))
                    return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (process != null)
                    process.destroy();
            }
        }

        return false;
    }

    /**
     * 获取当前设备的ip地址，该方法只能获取IPV4的地址
     * @param context
     * @return
     */
    public static String getIPAddress(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
        String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
        return ipAddress;
    }


}
